package com.tianzj.boot.mvc.controller.system;


import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.boot.mvc.entity.system.SysRole;
import com.tianzj.boot.mvc.service.system.SysRoleService;
import com.tianzj.boot.mvc.service.system.SysUserRoleService;
import com.tianzj.boot.util.AjaxJson;
import com.tianzj.boot.util.JsonUtil;
import com.tianzj.boot.util.PageVo.PageVo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("sysRole")
@Slf4j
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserRoleService sysUserRoleService;

	
	/**
	 * 跳转到角色列表页面
	 * @return
	 */
	@RequestMapping("roleList")
	public ModelAndView roleList(){
		return new ModelAndView("role/roleList");
	}

	/**
	 * 获取角色列表信息
	 * @param pageVo
	 * @return
	 */
	@RequestMapping("getRoleList")
	public String getSysRoleList(PageVo pageVo,SysRole sysRole){
		Pageable pageable=new PageRequest(pageVo.getPageNumber()-1,pageVo.getPageSize());
		Page<SysRole> roleList = sysRoleService.getSysRoleList(sysRole,pageable);
		return JsonUtil.list2BootStrapJson(roleList);
	}
	
	/**
	 * 获取角色列表信息
	 * @param pageVo
	 * @return
	 */
	@RequestMapping("getRoleListNoPage")
	public String getRoleListNoPage(){
		List<SysRole> roleList = sysRoleService.getRoleListNoPage();
		String json="";
		try {
			json= JsonUtil.objectToJson(roleList);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return json;
	}
	
	/**
	 * 添加角色信息
	 * @param SysRole
	 * @return
	 */
	@PostMapping("addSysRole")
	public ModelAndView addSysRole(SysRole sysRole){
		try {
			sysRole.setCreateDate(System.currentTimeMillis());
			sysRole.setStatus(1);
			sysRole.setCreateUser(1L);
			sysRoleService.addRole(sysRole);
		} catch (Exception e) {
			log.error(e.getMessage());
		}						
		return new ModelAndView("redirect:/sysRole/roleList");
	}
	/**
	 * 编辑角色信息
	 * @param SysRole
	 * @return
	 */
	@PostMapping("editSysRole")
	public ModelAndView editSysRole(SysRole sysRole){
		try {
			sysRole.setUpdateDate(System.currentTimeMillis());
			sysRoleService.updateRole(sysRole);
		} catch (Exception e) {
			log.error(e.getMessage());
		}						
		return new ModelAndView("redirect:/sysRole/roleList");
	}
	
	/**
	 * 根据id获取角色信息
	 * @param sysRole
	 * @return
	 */
	@PostMapping("getSysRoleById")
	public SysRole getSysRoleById(SysRole sysRole){
		return sysRoleService.getSysRoleById(sysRole.getId());
	}
	
	/**
	 * 删除角色信息
	 * @param sysRole
	 * @return
	 */
	@PostMapping("removeById")
	public AjaxJson removeById(String ids){
		AjaxJson json=new AjaxJson();
		try {
			json.setMsg("删除成功");
			//查询该角色是否已经使用，如果使用，先解除角色关系，在删除角色
			boolean flag = sysUserRoleService.getUserRoleByRoleIds(ids);
			if(!flag){
				json.setMsg("角色已经被使用,请先解除用户角色关系");
				return json;
			}
			sysRoleService.deleteSysRoleById(ids);
		} catch (Exception e) {
			log.error(e.getMessage());
			json.setMsg("删除失败");
		}
		return json;
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}
}
